package com.cskaoyan.service;

import com.cskaoyan.bean.MarketUser;
import com.cskaoyan.bean.MarketUserExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketUserMapper;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author stone
 * @date 2022/10/11 21:30
 */
@Service
public class UserServiceImpl implements UserService{

    @Autowired
    MarketUserMapper userMapper;

    /**
     * @param info  分页：page\limit  排序：sort\order，借助example
     * @param username
     * @param mobile
     * @return
     */
    @Override
    public CommonData<MarketUser> list(BasePageInfo info, String username, String mobile) {

        PageHelper.startPage(info.getPage(), info.getLimit());

        //只根据username和mobile做条件查询
        // where username like ? and mobile = ?
        //先不考虑username和mobile是否为空 → 接着考虑username是否为空，mobile是否为空
        // Criterion的list
        MarketUserExample example = new MarketUserExample();
        // 排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());

        MarketUserExample.Criteria criteria = example.createCriteria();
        // 增加if之后就变为了条件查询
        if (!StringUtil.isEmpty(username)) {
            criteria.andUsernameLike("%" + username + "%");
        }
        if (!StringUtil.isEmpty(mobile)) {
            criteria.andMobileEqualTo(mobile);
        }

        List<MarketUser> list = userMapper.selectByExample(example);

        PageInfo<MarketUser> pageInfo = new PageInfo<>(list);
        return CommonData.data(pageInfo);

    }


}
